/*	AnalysisWriter.java
 *
 *	Copyright (c) Manateem Challenge Group, Zaia Group, and contributors 2010
 *
 *	This file is part of Manatee ('this project').
 *
 *	This project is free software: you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, either version 3 of the License, or (at your option)
 *	any later version.
 *
 *	This project is distributed in the hope that it will be useful, but WITHOUT
 *	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *	more details.
 *
 *	You should have received a copy of the GNU General Public License along with
 *	this project. If not, see <http://www.gnu.org/licenses/>.
 */
package edu.bu.manateem.msg.analysis.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import edu.bu.manateem.msg.MSGConstants;

/**
 *	<p>
 *	
 *	</p>
 *
 *
 *	@author
 *		Christopher Jacobs
 *
 *	@version
 *		1.0
 *
 *	@changelog
 *		Jun 10, 2010	- Version 1.0.
 */
public abstract class AnalysisWriter
{
	/*
	 * TYPES
	 */

	/*
	 * CONSTANTS
	 */

	private static final String PARENT_DIR =
		MSGConstants.HOME_DIR + MSGConstants.FILE_SEPARATOR
			+ MSGConstants.APPLICATION_NAME;
	private static final String PLOTS_SUBDIR = "plots";
	
	/*
	 * FIELDS
	 */

	protected File directory;
	protected File plotsDir;
	
	/*
	 * CONSTRUCTORS
	 */

	public AnalysisWriter()
		throws FileNotFoundException
	{
		createDirectory();
		createPlotsDirectory();
	}
	
	/*
	 * INITIALIZERS
	 */

	/*
	 * METHODS
	 */

	private void createDirectory()
		throws FileNotFoundException
	{
		String currDir =
			new SimpleDateFormat("yyyy-MM-dd@HH_mm").format(new Date(
				System.currentTimeMillis()));
		directory =
			new File(AnalysisWriter.PARENT_DIR + MSGConstants.FILE_SEPARATOR
				+ currDir);
		if (!directory.mkdirs())
		{
			throw new FileNotFoundException(directory.getName());
		}
	}
	
	private void createPlotsDirectory()
		throws FileNotFoundException
	{
		try
		{
			String currDir = directory.getCanonicalPath();
			plotsDir =
				new File(currDir + MSGConstants.FILE_SEPARATOR
					+ AnalysisWriter.PLOTS_SUBDIR);
			if (!plotsDir.mkdirs())
			{
				throw new FileNotFoundException(directory.getName());
			}
		}
		catch (IOException x)
		{
			throw new FileNotFoundException("main putput directory not found!");
		}
	}
	
	public String getDirectory()
	{
		try
		{
			return directory.getCanonicalPath();
		}
		catch (IOException x)
		{
			return MSGConstants.USER_DIR;
		}
	}
}
